//
//  CCYNewfeatureCell.swift
//  03.微博
//
//  Created by 蔡承毅 on 16/9/24.
//  Copyright © 2016年 IOS. All rights reserved.
//

import UIKit

class CCYNewfeatureCell: UICollectionViewCell {
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// cell的indexpath
    var indexpath: IndexPath? {
        didSet{
            print("indexpath = \(indexpath)")
            backgroundImageView.image = UIImage(named: "new_feature_\(indexpath!.item + 1)")
            
            if indexpath?.item == NewFeatureCount - 1 {
                showButton(isHidden: false)
            }else{
                showButton(isHidden: true)
            }
        }
    }
    
    private func showButton(isHidden:Bool){
        shareButton.isHidden = isHidden
        startButton.isHidden = isHidden
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.contentView.addSubview(backgroundImageView)
        self.contentView.addSubview(shareButton)
        self.contentView.addSubview(startButton)
        
        self.backgroundImageView.translatesAutoresizingMaskIntoConstraints = false
        self.shareButton.translatesAutoresizingMaskIntoConstraints = false
        self.startButton.translatesAutoresizingMaskIntoConstraints = false
        
        self.contentView.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: .top, relatedBy: .equal, toItem: self.contentView, attribute: .top, multiplier: 1, constant: 0))
        self.contentView.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: .leading, relatedBy: .equal, toItem: self.contentView, attribute: .leading, multiplier: 1, constant: 0))
        self.contentView.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: .bottom, relatedBy: .equal, toItem: self.contentView, attribute: .bottom, multiplier: 1, constant: 0))
        self.contentView.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: .trailing, relatedBy: .equal, toItem: self.contentView, attribute: .trailing, multiplier: 1, constant: 0))
        
        // 开始体验按钮
        self.contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: .centerX, relatedBy: .equal, toItem: self.contentView, attribute: .centerX, multiplier: 1, constant: 0))
        self.contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: .bottom, relatedBy: .equal, toItem: self.contentView, attribute: .bottom, multiplier: 1, constant: -130))
        
        // 分享按钮
        self.contentView.addConstraint(NSLayoutConstraint(item: shareButton, attribute: .centerX, relatedBy: .equal, toItem: self.contentView, attribute: .centerX, multiplier: 1, constant: 0))
        self.contentView.addConstraint(NSLayoutConstraint(item: shareButton, attribute: .bottom, relatedBy: .equal, toItem: startButton, attribute: .top, multiplier: 1, constant: -16))
    }
    
    func shareButtonDidClick() -> Void {
        shareButton.isSelected = !shareButton.isSelected
    }
    
    func startButtonDidClick() -> Void {
        UIApplication.shared.keyWindow?.rootViewController = CCYTabbarViewController()
    }
    
    // MARK: 懒加载
    /// 背景
    lazy var backgroundImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    
    /// 分享按钮
    lazy var shareButton: UIButton = {
        let button = UIButton()
        
        button.setTitle("分享给大家", for: .normal)
        button.setTitleColor(UIColor.black, for: .normal)
        
        button.setImage(UIImage(named:"new_feature_share_false"), for: .normal)
        button.setImage(UIImage(named:"new_feature_share_true"), for: .selected)
        
        button.addTarget(self, action: #selector(shareButtonDidClick), for: .touchUpInside)
        return button
    }()
    
    /// 开始按钮
    lazy var startButton: UIButton = {
        let button = UIButton()
        
        button.setTitle("开始体验", for: .normal)
        
        button.setBackgroundImage(#imageLiteral(resourceName: "new_feature_finish_button"), for: .normal)
        button.setBackgroundImage(UIImage(named:"new_feature_finish_button_highlighted"), for: .highlighted)
        
        button.addTarget(self, action: #selector(startButtonDidClick), for: .touchUpInside)
        return button
    }()
    
    
}
